package com.zbf.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Coordinate;
import net.coobird.thumbnailator.geometry.Positions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: LCG
 * @Date: 2021-11-29 10:07:46
 * @description: 上传图片的压缩处理
 **/
@Component
public class ImageHandlerUtils {
    
    /** 
     * @author: LCG
     * @date: 2021/11/29
     * @description:  解析HTML中的图片链接
     */
    public List<String> getImageUrlFromHtml(String html){
        List<String> list=new ArrayList<>();
        Document parse = Jsoup.parse(html);
        Elements img = parse.getElementsByTag("img");
        for (Element element : img) {
            list.add(element.attr("src"));
        }
        return list;
    }
    
    /** 
     * @author: LCG
     * @date: 2021/11/29
     * @description:  设置商品html中的图片链接
     */
    public String setImageUrlFromHtml(String html,List<String> imagesSrc){
        Document parse = Jsoup.parse(html);
        Elements img = parse.getElementsByTag("img");
        for (int i = 0; i < img.size(); i++) {
            img.get(i).attr("src",imagesSrc.get(i));
        }
        return parse.body().html();
    }

    /** 
     * @author: LCG
     * @date: 2021/11/29
     * @description:  图片压缩
     */
    public File imageCompress(File imageFile,double yasuobi,double zhiliang){



        return null;
    }


    public Byte[] imageByteCompress(Byte[] bytes){
        return null;
    }

    /** 
     * @author: LCG
     * @date: 2021/11/29
     * @description:  把图片印刷到图片上
     */
    public static void shuiYinChuLi(InputStream is, File markImg, String path) throws IOException {
        BufferedImage image = ImageIO.read(is);
        BufferedImage markImage = ImageIO.read(markImg);
        //原图的宽高
        int imgWidth = image.getWidth(null);
        int imgHeight = image.getHeight(null);
        //水印图片的宽高
        int markWidth = markImage.getWidth(null);
        int markHeight = markImage.getHeight(null);

        //计算输出水印图片的位置x和y轴
        int mark_x = imgWidth - imgWidth / 9;
        int mark_y = imgWidth / 9-imgWidth / 10;
        //计算输出水印图片的大小
        int mark_width = imgWidth / 10;
        int mark_height = (imgWidth * markHeight) / (10 * markWidth);

        //将水印图片压缩成输出的大小
        markImage = Thumbnails.of(markImage).size(mark_width,mark_height).asBufferedImage();

        //watermark(位置，水印图，透明度0.5f=50%透明度)
        //outputQuality（控制图片的质量，1f=100%高质量）
        Thumbnails.of(image).size(imgWidth, imgHeight)
                .watermark(new Coordinate(mark_x,mark_y), markImage, 1f)
                .outputQuality(1f).toFile(path);

    }



    /**
     * @author: LCG
     * @date: 2021/11/29
     * @description:  图片添加一个挂角的小图
     */
    public static void shuiYinChuLi2(String outPath, File markImg) throws IOException {
        // ImageIO读取图片
        BufferedImage image = ImageIO.read(markImg);

        Thumbnails.of(image)
                // 设置图片大小
                .size(image.getWidth(), image.getHeight())
                // 加水印 参数：1.水印位置 2.水印图片 3.不透明度0.0-1.0
                .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(markImg),0.5f)
                // 输出到文件
                .toFile(outPath);
    }

}
